# -*- coding: utf-8 -*-
# coding:utf8

import sys
import json
from locust import HttpLocust, TaskSet, task


__author__ = 'longyiqun'

# UTF-8 reload
reload(sys)
sys.setdefaultencoding('utf-8')


# 定义用户行为
# 继承自TaskSet
# on_start为第一次call的函数
# @task为随机call的地址, 1,2代表call的比例
# WebsiteUser定义call的行为, 定义call的地址, 每次call的间隔时间, 为随机1s到5s之间


class BixinApiTasks(TaskSet):

    @task
    def accountinfo(self):
        reqBody = '{ "app": "BIXIN",' \
                  '"tradeNo" : "234234234234234",' \
                  '"userId" : "d5dae7ea3a75c73af68d5c4f4c2d4035","notifyUrl" : "", "returnUrl" : "", "clientIp" : "10.12.12.12", "platform" : "APP", "channel" : "MANUAL", "target" : "BALANCE_RECHARGE", "amountPay" : 50.0, "amountDiscount" : 50.0, "amountTarget" : 100.0}'

        response = self.client.post("/account/info", reqBody, headers={'Content-Type': 'application/json'})

        print("Response status code:", response.status_code)
        print("Response content:", response.content)

    # @task
    # def rechargemoney(self):
        # reqBody = '{ "app" : "BIXIN", "tradeNo" : "234234234234234", "userId" : "d5dae7ea3a75c73af68d5c4f4c2d4035", "notifyUrl" : "", "returnUrl" : "", "clientIp" : "10.12.12.12", "platform" : "APP", "channel" : "MANUAL", "target" : "BALANCE_RECHARGE", "amountPay" : 50.0, "amountDiscount" : 50.0, "amountTarget" : 100.0}'
        # r = self.client.post("/recharge", {
        #     "app": "BIXIN",
        #     "tradeNo": "234234234234234",
        #     "userId": "d5dae7ea3a75c73af68d5c4f4c2d4035",
        #     "notifyUrl": "",
        #     "returnUrl": "",
        #     "clientIp": "10.12.12.12",
        #     "platform": "APP",
        #     "channel": "MANUAL",
        #     "target": "BALANCE_RECHARGE",
        #     "amountPay": 50.0,
        #     "amountDiscount": 50.0,
        #     "amountTarget": 100.0
        # })

        # 定义requests的请求头
        # header = {"Content-Type": "application/json"}
        #
        # r = self.client.post("/account/vip/diamond/info", {
        #     "app": "BIXIN",
        #     "userId": "d5dae7ea3a75c73af68d5c4f4c2d4035"
        # }, headers=header)
        # # 返回json
        # resultJson = r.json()
        # code = resultJson["code"]
        # data = resultJson["data"]
        #
        # print resultJson
        # print code
        # print data
        #
        # assert resultJson["code"] == 200

        # if code != 200:
        #     resultJson.failure("Wrong")


class WebsiteUser(HttpLocust):
    task_set = BixinApiTasks
    host = "http://10.28.28.132:10011"
    #host = "http://test-gateway.bxapp.cn:10050"
    min_wait = 1000
    max_wait = 3000
